(function($){
	//定义Dialog的构造方法
	var Dialog = function(ele, opt){
		_that = ele,
        this.defaults = {
        	speed:600,//滑动的速度
        	titleFlag:false,//是否显示头部
        	mainFlag:true,//是否显示主体内容
			footerFlag:true,//是否显示底部
			closeFlag:false,//是否显示关闭按钮
			cancelFlag:false,//是否显示取消按钮
			dialogbgFlag:true,//是否显示遮罩层
			dialogbgClick:false,//点击遮罩层是否能关闭
        	title_name:'温馨提示',//头部内容
        	agreedName:'确定',
        	cancelName:'取消',
			msg:'',//主体内容信息
			agreedFn:function () {},//同意按钮的事件
			cancelFn:function(){}//取消按钮事件
        },
        this.options = $.extend({}, this.defaults, opt);
	}
	Dialog.prototype = {
		init:function(){
			var W_w = $(window).width(),W_h = $(window).height();
			if(!$(".dialog").length){
				this.dialog(W_w,W_h);
				return this;
			}
			
		},
		//生成html
		dialog:function(width,height){
			var dialog_bg = $('<div class="dialog_bg"></div>'),
				dialog = $('<div class="dialog"></div>'),
				close = $('<div class="dialog_close">X</div>'),
				title = $('<div class="dialog_title">'+ this.options.title_name +'</div>'),
				main = $('<div class="dialog_main">'+ this.options.msg +'</div>'),
				footer = $('<div class="dialog_foot"></div>'),
				agreed = $('<button>'+this.options.agreedName+'</button>'),
				cancel = $('<button>'+this.options.cancelName+'</button>'),
				agreedfn = this.options.agreedFn,
				cancelFn = this.options.cancelFn,
				dialogbgClick = this.options.dialogbgClick;

			if(this.options.titleFlag){
				dialog.append(title);
			}
			if(this.options.closeFlag){
				dialog.append(close);
			}
			if(this.options.mainFlag){
				dialog.append(main);
				if(!this.options.titleFlag && !this.options.footerFlag && !this.options.closeFlag){
					main.addClass('radius');
				}
			}
			if(this.options.footerFlag){
				dialog.append(footer);
				footer.append(agreed);
				if(this.options.cancelFlag){
					footer.append(cancel);
				}else{
					agreed.css({margin:"0 80px"});
				}
			}
			if(this.options.dialogbgFlag){
				$("body").append(dialog_bg);
			}
			$("body").append(dialog);

			dialog_bg.css({width:width,height:height});
			dialog.animate({top:"20%"}, this.options.speed);

			//点击取消
			cancel.click(function(event) {
				if (typeof(cancelFn) == 'function') {
			        cancelFn();
			    }
				remove();
			});
			//点击关闭
			close.click(function(event) {
				remove();
			});
			//点击同意
			agreed.click(function(){
				
				if (typeof(agreedfn) == 'function') {
			        agreedfn();
			     }
			    remove();
			});

			//点击背景
			dialog_bg.click(function(){
				if(dialogbgClick){
					remove();
				}
			});

			//移除
			function remove() {
				dialog_bg.remove();
				dialog.remove();
			}

		}
	}
	$.fn.dialog = function(options){
		//创建Dialog的实体
        var diaLog = new Dialog(this, options);
        //调用其方法
        return diaLog.init();
	}
	
})(jQuery);

$(function(){
	$(window).resize(function(){
        if($(".dialog_bg")){
            var W_w = $(window).width(),W_h = $(window).height();
            $(".dialog_bg").css({width:W_w,height:W_h});
        }
    });
})